package com.seat.utils;

import com.seat.pojo.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Random;
import java.util.UUID;

//email的生成与验证
@Slf4j
public class EmailUtil {

//
    @Resource
    public static JavaMailSender javaMailSender;

    //读取yml文件中username的值并赋值给form
    @Value("${spring.mail.username}")
    private static String from;

    public static String SendEemail(String email)
    {

//        编写一篇简单的邮件
        SimpleMailMessage mailMessage=new SimpleMailMessage();
//        标题
        mailMessage.setSubject("验证码");
//        邮件内容（随机生成验证码）
        String code= UUID.randomUUID().toString().substring(0,5);
        mailMessage.setText(code);
//        发送人必须与配置文件中的数据相同
        mailMessage.setFrom(from);
//        收件人
        mailMessage.setTo(email);
//        发送

            javaMailSender.send(mailMessage);

        return code;
    }

}

